package models

import (
	"go-admin/global/orm"
	"go-admin/tools"
	"time"
	_ "time"
)

type ZxRevisit struct {
	Id             int       `json:"id" gorm:"type:int(11) unsigned;primary_key"`         //
	RevisiterId    int       `json:"revisiterId,omitempty" gorm:"type:int(11) unsigned;"` // 回访者id
	RevisitState   string    `json:"revisitState,omitempty"`
	SatisfiedState string    `json:"satisfiedState" gorm:"type:varchar(10);"` // 满意度
	CustomSuggest  string    `json:"customSuggest" gorm:"type:varchar(200);"` // 客户意见
	FinishDate     time.Time `json:"finishDate" gorm:"type:timestamp;"`       // 完成日期
	Note           string    `json:"note" gorm:"type:varchar(200);"`          // 备注
	CreateBy       string    `json:"createBy" gorm:"type:varchar(128);"`      // 创建者
	UpdateBy       string    `json:"updateBy" gorm:"type:varchar(128);"`      // 更新者
	DataScope      string    `json:"dataScope" gorm:"-"`
	Params         string    `json:"params"  gorm:"-"`
	BaseModel
}

func (ZxRevisit) TableName() string {
	return "zx_revisit"
}

// 创建ZxRevisit
func (e *ZxRevisit) Create() (ZxRevisit, error) {
	var doc ZxRevisit
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取ZxRevisit
func (e *ZxRevisit) Get() (ZxRevisit, error) {
	var doc ZxRevisit
	table := orm.Eloquent.Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	//if e.CustomId != 0 {
	//	table = table.Where("custom_id = ?", e.CustomId)
	//}

	//if e.Revisiter != "" {
	//	table = table.Where("revisiter like ?", "%"+e.Revisiter+"%")
	//}

	//if e.RevisitState != "" {
	//	table = table.Where("revisit_state = ?", e.RevisitState)
	//}

	//if e.FinishState != "" {
	//	table = table.Where("finish_state = ?", e.FinishState)
	//}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取ZxRevisit带分页
func (e *ZxRevisit) GetPage(pageSize int, pageIndex int) ([]ZxRevisit, int, error) {
	var doc []ZxRevisit

	table := orm.Eloquent.Select("*").Table(e.TableName())

	//if e.CustomId != 0 {
	//	table = table.Where("custom_id = ?", e.CustomId)
	//}

	//if e.Revisiter != "" {
	//	table = table.Where("revisiter like ?", "%"+e.Revisiter+"%")
	//}

	//if e.RevisitState != "" {
	//	table = table.Where("revisit_state = ?", e.RevisitState)
	//}

	//if e.FinishState != "" {
	//	table = table.Where("finish_state = ?", e.FinishState)
	//}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新ZxRevisit
func (e *ZxRevisit) Update() (update ZxRevisit, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", e.Id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除ZxRevisit
func (e *ZxRevisit) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&ZxRevisit{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *ZxRevisit) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&ZxRevisit{}).Error; err != nil {
		return
	}
	Result = true
	return
}
